import { defineConfig, loadEnv } from 'vite';
import path from 'path';
import vue from '@vitejs/plugin-vue';
import { resolve } from 'path';
import vueJsx from '@vitejs/plugin-vue-jsx';
import 'terser';

export default ({ mode }) => {
    const env = loadEnv(mode, process.cwd());
    const proxyPrefix = env.VITE_APP_PROXY_PREFIX;

    return defineConfig({
        base: env.VITE_APP_BASE,
        css: {
            modules: {
                localsConvention: 'camelCaseOnly'
            },
            preprocessorOptions: {
                less: {
                    modifyVars: {
                        'primary-color': '#2186FB',
                        // 配置我们定义的less样式变量
                        hack: `true; @import (reference) "${path.resolve(
                            './src/themes/variable.less'
                        )}";`
                    },
                    javascriptEnabled: true
                }
            }
        },
        plugins: [vue(), vueJsx()],
        resolve: {
            alias: {
                '@': resolve(__dirname, 'src'),
                '@cps': resolve(__dirname, 'src/components'),
                'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js'
            }
        },

        build: {
            chunkSizeWarningLimit: 1500,
            // minify: 'terser',
            // terserOptions: {
            //     compress: {
            //         // 开启代码压缩
            //         drop_console: true, // 移除console
            //         drop_debugger: true // 移除debugger
            //     },
            //     mangle: {
            //         // 混淆变量名
            //         properties: {
            //             regex: /^_/
            //         }
            //     }
            // },
            target: 'esnext',
            rollupOptions: {
                output: {
                    manualChunks(id) {
                        if (id.includes('node_modules') && id.includes('js')) {
                            return id
                                .toString()
                                .split('node_modules/')[1]
                                .split('/')[0]
                                .toString();
                        }
                    }
                }
            }
            // rollupOptions: {
            //   output: {
            //     manualChunks(id) {
            //       if (id.includes('node_modules')) {
            //         return id.toString().split("node_modules/")[1].split("/")[0].toString();
            //       }
            //     }
            //   }
            // }
        },

        server: {
            host: '0.0.0.0',
            port: env.VITE_APP_PORT || process.env.port,
            proxy: {
                [proxyPrefix]: {
                    target: env.VITE_APP_BASE_URL,
                    changeOrigin: true,
                    ws: true,
                    toProxy: true,
                    rewrite: (path) =>
                        path.replace(new RegExp(`^${proxyPrefix}`), '')
                }
            }
        }
    });
};
